package cn.bupt.remotelab.http
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;
	
	public class MyHttpOperator extends Sprite
	{
		private var _loader:URLLoader;
		public function MyHttpOperator(fmt:String){
			_loader = new URLLoader(); 
			configureListeners();
			_loader.dataFormat = fmt; 
		}
		public function getLoadedData():*{
			return _loader.data;
		}
		public function load(req:URLRequest):void{
			try {
				_loader.load(req);
			} catch (error:Error) {
				trace("Unable to load requested document.");
			}
		}
		private function configureListeners():void {
			_loader.addEventListener(Event.COMPLETE, completeHandler);
			_loader.addEventListener(Event.OPEN, openHandler);
			_loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		private function completeHandler(event:Event):void {
			trace("completeHandler: " + _loader.data);
			dispatchEvent(event);
		}
		private function openHandler(event:Event):void {
			trace("openHandler: " + event);
		}
		private function progressHandler(event:ProgressEvent):void {
			trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
		}
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}
		private function httpStatusHandler(event:HTTPStatusEvent):void {
			trace("httpStatusHandler: " + event);
		}
		private function ioErrorHandler(event:IOErrorEvent):void {
			trace("ioErrorHandler: " + event);
		} 
	}
}
